<template>
	<view class="home">
		<view class="abr">
			<view class="abr-l">
				<view class="abritem">
					<view class="abrtext">
						系数a：
					</view>
					<view class="abrinput">
						<uni-easyinput v-model="couform.countera" type="digit" :styles="styles"
							:placeholderStyle="placeholderStyle" placeholder="a系数范围：0.0-1.0"></uni-easyinput>
					</view>
				</view>
				<view class="abritem">
					<view class="abrtext">
						系数b：
					</view>
					<view class="abrinput">
						<uni-easyinput v-model="couform.counterb" type="digit" :styles="styles"
							:placeholderStyle="placeholderStyle" placeholder="b系数范围：0.0-1.0"></uni-easyinput>
					</view>

				</view>
				<view class="abritem">
					<view class="abrtext">
						系数C：
					</view>
					<view class="abrinput">
						<uni-easyinput v-model="couform.counterr" type="digit" :styles="styles"
							:placeholderStyle="placeholderStyle" placeholder="C系数范围：1-100"></uni-easyinput>
					</view>
				</view>
			</view>
		</view>
		<view class="abrbutt">
			<view class="abrbox">
				<view class="abrbutext">
					<view class="" style="font-size: 65rpx;color: #2e6d79;">
						{{fif}}
					</view>
					<view class="" style="font-size: 45rpx;color: #2e6d79;">
						当前编号
					</view>
				</view>
				<view class="abrflx">
					<view class="botn" v-for="(item,index) in buttnum" :key="index">
						<u-button @click="abritembb(item)" type="primary" size="small"
							color="#327983">{{item.name}}</u-button>
					</view>
				</view>
			</view>

			<view class="" style="font-size: 35rpx;color: #3a3a3a;letter-spacing: 20rpx;">
				点击编码获取流速仪参数
			</view>
			<view class="" style="font-size: 25rpx;color: #adadad;letter-spacing: 10rpx;">
				终端设备内置9个流速仪公式
			</view>
		</view>
		<!-- 连接设备 -->
		<view class="condev">
			<view class="yuanIf" @click="writenuDevFn" hover-class="yuanbtnIf" v-if="!sheb">
				<view class="" style="font-size: 50rpx;">
					连接
				</view>
				<view class="" style="font-size: 50rpx;">
					设备
				</view>
			</view>
			<view class="yuan" @click="writenubaa" hover-class="yuanbtn" v-if="sheb">
				<view class="" style="font-size: 50rpx;">
					{{sheb}}
				</view>
				<view class="">
					通讯地址
				</view>
			</view>
			<view class="fang">
				<view class="fangtext" hover-class="fangbut" @click="inpuaa">
					修改参数
				</view>
				<view class="fanginput">
					<uni-data-select v-model="value1" :localdata="range" @change="changeFn"></uni-data-select>
				</view>
			</view>
		</view>
		<view class="formula">
			<view class="formulatop">
				流速公式：V=a+bn，n=N*C/T
			</view>
			<view class="formulabom">
				<view class="formulaitem">
					a：仪器常数
				</view>
				<view class="formulaitem">
					b：水力螺距
				</view>
				<view class="formulaitem">
					C：转数比
				</view>
				<view class="formulaitem">
					n：转 速
				</view>
				<view class="formulaitem">
					N：信号数
				</view>
				<view class="formulaitem">
					T：信号时长
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value1: '',
				range: [{
						value: '00',
						text: '#1'
					},
					{
						value: '06',
						text: '#2'
					},
					{
						value: '0c',
						text: '#3'
					},
					{
						value: '12',
						text: '#4'
					},
					{
						value: '18',
						text: '#5'
					},
					{
						value: '1e',
						text: '#6'
					},
					{
						value: '24',
						text: '#7'
					},
					{
						value: '2a',
						text: '#8'
					},
					{
						value: '30',
						text: '#9'
					},
				],
				placeholderStyle: "color:#c1c1c1;font-size:14px",
				vuexlist: {

				},
				abriteminfo: '',
				infoaa: true,
				infobb: true,
				fanti: true,
				modifynum: '00',
				buttnum: [{
						id: '00',
						name: '#1'
					},
					{
						id: '06',
						name: '#2'
					},
					{
						id: '0c',
						name: '#3'
					},
					{
						id: '12',
						name: '#4'
					},
					{
						id: '18',
						name: '#5'
					},
					{
						id: '1e',
						name: '#6'
					},
					{
						id: '24',
						name: '#7'
					},
					{
						id: '2a',
						name: '#8'
					},
					{
						id: '30',
						name: '#9'
					},
				],
				fif: '#9',
				styles: {
					color: '#999999',
					borderColor: '#c3c3c3'
				},
				num: 50,
				lists: [],
				list: [],
				deviceId: '',
				serviceId: '',
				characteristics: [],
				characteristicId: '',
				iceinfo: false,
				couform: {
					countera: '',
					counterb: '',
					counterr: '',
				},
				resHexitem: '',
				// msgall: []
				msgall: ['', '0x03', '0x10', '0x00', '0x00', '0x06', '', ''],
				jis: '',
				cr: '',
				fff: '',
				abrnum: '00',
				sheb: '',
				countcrca: '',
				countcrcb: '',
				countcrcc: '',
				countcrcd: '',
				countcrce: '',
				countcrcf: '',
				countall: ['', '0x10', '0x10', '0x00', '0x00', '0x06', '0x0c', '', '', '', '', '', '', '0x00', '0x00',
					'0x00', '0x00', '0x00', '0x00', '', ''
				]
			}
		},
		onLoad(options) {
			
		},
		onShow() {
			this.deviceId = this.vuex_deviceId
			this.getBLEDeviceServices()
			if(this.vuex_devicename != ''){
				uni.showLoading({
					title: '设备连接中...'
				});
				setTimeout(() => {
					uni.hideLoading()
				}, 3000)
			}
		},
		destroyed() {
			console.log('销毁');
		},
		methods: {
			changeFn(e) {
				this.modifynum = e
			},
			//获取蓝牙的所有服务
			getBLEDeviceServices() {
				console.log('所有服务');
				setTimeout(() => {
					uni.getBLEDeviceServices({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: this.vuex_deviceId,
						success: (res) => {
							res.services.forEach((item) => {

								if (item.uuid.indexOf("0000FFF0") != -1) {
									this.serviceId = item.uuid;
									//获取特征
									this.getBLEDeviceCharacteristics()
								}
								if (item.uuid.indexOf("0000FFE0") != -1) {
									this.serviceId = item.uuid;
									//获取特征
									this.getBLEDeviceCharacteristics()
								}
								if (item.uuid.indexOf("00001000") != -1) {
									this.serviceId = item.uuid;
									//获取特征
									this.getBLEDeviceCharacteristics()
								}
							})
						}
					})
				}, 1500)
			},
			//获取蓝牙特征
			getBLEDeviceCharacteristics() {
				console.log("进入特征");

				setTimeout(() => {
					uni.getBLEDeviceCharacteristics({
						// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
						deviceId: this.deviceId,
						// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
						serviceId: this.serviceId,
						success: (res) => {
							this.characteristics = res.characteristics
							res.characteristics.forEach((item) => {

								if (item.uuid.indexOf("FFF2") != -1) {
									this.fff = item.uuid
								}
								if (item.uuid.indexOf("FFE1") != -1) {
									this.fff = item.uuid
								}
								if (item.uuid.indexOf("0000FFE1") != -1) {
									this.characteristicId = item.uuid
									//利用传参的形势传给下面的notify，这里的uuid如果都需要用到，就不用做判断了，建议使用setTimeout进行间隔性的调用此方法
									this.notifyBLECharacteristicValueChange(item.uuid)
								}
								if (item.uuid.indexOf("0000FFF1") != -1) {
									this.characteristicId = item.uuid
									this.notifyBLECharacteristicValueChange(item.uuid)
								}
								if (item.uuid.indexOf("00001002") != -1) {
									this.characteristicId = item.uuid
									this.notifyBLECharacteristicValueChange(item.uuid)
								}
							})
						},
						fail: (res) => {
							console.log(res)
						}
					})
				}, 1000)
			},
			// 启用 notify 功能
			notifyBLECharacteristicValueChange(characteristicId) {
				uni.notifyBLECharacteristicValueChange({
					state: true, // 启用 notify 功能
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: this.deviceId,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: this.serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId: this.characteristicId,
					success: (res) => {
						console.log('启用功能success', res.errMsg)
						// vuex
						uni.$u.vuex('vuex_devicename', this.vuexlist.name)
						uni.hideLoading()
						this.listenValueChange()
						uni.showToast({
							title: '设备已连接',
							icon: 'success',
							mask: true,
							duration: 1000
						});
						setTimeout(()=>{
							this.writenubaa()
						},500)
						

					},
					fail: (res) => {
						console.log('启用功能fail', res)
						uni.hideLoading()
						uni.showToast({
							title: '获取服务失败',
							icon: 'fail',
							mask: true,
							duration: 2000
						});

					}
				})

			},
			// 【9】监听消息变化
			listenValueChange() {
				setTimeout(() => {
					uni.onBLECharacteristicValueChange(res => {
						setTimeout(() => {
							uni.$u.vuex('vuex_couform', this.couform)
						}, 500)
						console.log('RES', res)
						let resHex = this.ab2hex(res.value)
						let device = resHex[8] + resHex[9]
						console.log('初始化数据----------', resHex);
						let slicest = resHex.slice(0, 4)

						let modifysty = resHex.slice(2, 4)
						console.log('转换字符', modifysty);
						if (slicest.indexOf("0003") == -1 && modifysty !== '10') {
							this.gaininfo(resHex)
							this.infobb = false
							uni.hideLoading()
						}
						if (slicest.indexOf("0003") !== -1 && modifysty !== '10') {
							this.listenValueinfo(device)
							this.infoaa = false
							uni.hideLoading()
						}
						if (modifysty == 10) {
							this.fanti = false
							uni.hideLoading()
						}
					})
				}, 300)

			},
			// 初始化信息处理
			listenValueinfo(resHex) {
				this.resHexitem = resHex
				this.sheb = parseInt(resHex, 16)
			},
			// 获取信息处理
			gaininfo(resHex) {
				let numa = resHex[6] + resHex[7] + resHex[8] + resHex[9]
				let aa = parseInt(numa, 16) / 10000
				this.couform.countera = aa.toFixed(4)

				let numb = resHex[10] + resHex[11] + resHex[12] + resHex[13]
				let bb = parseInt(numb, 16) / 10000
				this.couform.counterb = bb.toFixed(4)

				let numr = resHex[14] + resHex[15] + resHex[16] + resHex[17]
				let rr = parseInt(numr, 16)
				this.couform.counterr = rr.toFixed(4)
			},
			// ArrayBuffer转16进度字符串示例
			ab2hex(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice(-2)
					}
				)
				return hexArr.join('')
			},
			// crc校验
			crc16up(data) {
				var res = this.crc16(Buffer.from(data, "hex"));
				this.cr = res
			},
			crc16(data) {
				let crcValue = 0xFFFF;
				for (let i = 0; i < data.length; i++) {
					crcValue ^= data[i] & 0xFFFF
					for (let j = 0; j < 8; j++) {
						if (crcValue & 0x0001) {
							crcValue >>= 1
							crcValue ^= 0xA001
						} else {
							crcValue >>= 1
						}
					}
				}

				let crcstr = crcValue.toString(16).toUpperCase();
				if (crcstr.length % 2 == 0) {
					let crcArr = crcstr.match(/.{1,2}/g)
					console.log('CRC16验证', crcArr);

					let arr = new Array(2)
					arr[0] = crcArr[1]
					arr[1] = crcArr[0]
					return arr
					// return result;
				} else {
					let str = '0' + crcstr
					let crcArr = str.match(/.{1,2}/g)
					console.log('CRC16验证', crcArr);
					let arr = new Array(2)
					arr[0] = crcArr[1]
					arr[1] = crcArr[0]
					return arr
				}
			},
			// #1-#9
			abritembb(item) {
				if (this.sheb == '') {
					uni.showToast({
						title: '设备未连接',
						icon: 'fail',
						mask: true,
						duration: 1500
					});
					return
				}
				this.abritem(item)
				setTimeout(() => {
					console.log('停止循环');
					if (this.infobb) {
						this.infobb = false
						uni.hideLoading()
						uni.showToast({
							title: '获取失败',
							icon: 'fail',
							mask: true,
							duration: 2000
						});
						return
					}
				}, 10000)
			},
			abritem(item) {
				uni.showLoading({
					title: '获取中',
					mask: true
				});
				this.infobb = true
				this.abriteminfo = item
				this.modifynum = item.id
				this.couform.countera = ''
				this.couform.counterb = ''
				this.couform.counterr = ''
				// this.abrnum = id
				// this.msgall.length = 0
				this.fif = item.name
				this.value1 = item.id
				this.writeBLECharacteristicValue(item.id)
			},
			//读取蓝牙
			writeBLECharacteristicValue(id) {
				let iditem = id
				// let msgall = ['','0x03','0x10','0x00','0x00','0x06','','']
				this.jis = this.resHexitem + '03' + '10' + iditem + '00' + '06'
				this.crc16up(this.jis)
				this.msgall[0] = '0x' + this.resHexitem
				this.msgall[6] = '0x' + this.cr[0]
				this.msgall[7] = '0x' + this.cr[1]
				this.msgall[3] = '0x' + iditem
				// msg = [0x22,0x03,0x10,0x00,0x00,0x06,0xc6,0x5b]
				// let msg = JSON.parse( msgall )
				setTimeout(() => {
					if (this.msgall[0] !== '') {
						const buffer = new ArrayBuffer(8)
						const dataView = new DataView(buffer)
						// dataView.setUint8(0, 0)

						dataView.setUint8(0, this.msgall[0]);
						dataView.setUint8(1, this.msgall[1]);
						dataView.setUint8(2, this.msgall[2]);
						dataView.setUint8(3, this.msgall[3]);
						dataView.setUint8(4, this.msgall[4]);
						dataView.setUint8(5, this.msgall[5]);
						dataView.setUint8(6, this.msgall[6]);
						dataView.setUint8(7, this.msgall[7]);
						// for (var i = 0; i < msg.length; i++) {
						// 	dataView.setUint8(i, msg.charAt(i).charCodeAt())
						// }
						uni.writeBLECharacteristicValue({
							deviceId: this.deviceId,
							serviceId: this.serviceId,
							// characteristicId: '0000FFF2-0000-1000-8000-00805F9B34FB',
							characteristicId: this.fff,
							value: buffer,
							success: (res) => {
								console.log('读取发送成功', this.msgall)
								setTimeout(() => {
									console.log('循环');
									if (this.infobb) {
										this.abritem(this.abriteminfo)
									}
								}, 500)
							},
							fail: (res) => {
								if (res.errCode == 10006) {
									uni.hideLoading()
									uni.showToast({
										title: '当前连接已断开',
										icon: 'success',
										mask: true,
										duration: 2000
									});
								}
								if (res.errCode == 10004) {
									uni.hideLoading()
									uni.showToast({
										title: '未找到服务',
										icon: 'success',
										mask: true,
										duration: 2000
									});
								}
								if (res.errCode == 10005) {
									uni.hideLoading()
									uni.showToast({
										title: '未找到特征值',
										icon: 'success',
										mask: true,
										duration: 2000
									});
								}
							}
						})
					}
				}, 500)
			},
			// 连接设备
			writenuDevFn(){
				uni.navigateTo({
					url: '/pages/index/devBEL'
				});
			},
			// 初始化设备ID
			writenubaa() {
				console.log('初始化');
				this.writenub()
				setTimeout(() => {
					console.log('停止循环');
					if (this.infoaa) {
						this.infoaa = false
						uni.hideLoading()
						uni.showToast({
							title: '连接失败',
							icon: 'fail',
							mask: true,
							duration: 2000
						});
						return
					}
				}, 10000)
			},
			writenub() {
				uni.showLoading({
					title: '获取中',
					mask: true
				});
				this.infoaa = true
				let msg = ['0x00', '0x03', '0xff', '0x02', '0x00', '0x01', '0x14', '0x0f']
				const buffer = new ArrayBuffer(8)
				const dataView = new DataView(buffer)

				dataView.setUint8(0, msg[0]);
				dataView.setUint8(1, msg[1]);
				dataView.setUint8(2, msg[2]);
				dataView.setUint8(3, msg[3]);
				dataView.setUint8(4, msg[4]);
				dataView.setUint8(5, msg[5]);
				dataView.setUint8(6, msg[6]);
				dataView.setUint8(7, msg[7]);

				uni.writeBLECharacteristicValue({
					deviceId: this.deviceId,
					serviceId: this.serviceId,
					characteristicId: this.fff,
					value: buffer,
					success: (res) => {
						console.log('写入', res)
						setTimeout(() => {
							console.log('循环');
							if (this.infoaa) {
								this.writenub()
							}
						}, 500)
					},
					fail: (res) => {
						if (res.errCode == 10006) {
							uni.hideLoading()
							uni.showToast({
								title: '当前连接已断开',
								icon: 'success',
								mask: true,
								duration: 2000
							});
						}
						if (res.errCode == 10004) {
							uni.hideLoading()
							uni.showToast({
								title: '未找到服务',
								icon: 'success',
								mask: true,
								duration: 2000
							});
						}
						if (res.errCode == 10005) {
							uni.hideLoading()
							uni.showToast({
								title: '未找到特征值',
								icon: 'success',
								mask: true,
								duration: 2000
							});
						}
					}
				})
			},
			duankai() {
				uni.closeBLEConnection({
					deviceId: this.deviceId,
					success(res) {
						uni.showToast({
							title: '蓝牙已断开',
							icon: 'success',
							mask: true,
							duration: 2000
						});
					}
				})
			},
			inpuaa() {
				if (this.sheb == '') {
					uni.showToast({
						title: '设备未连接',
						icon: 'fail',
						mask: true,
						duration: 1500
					});
					return
				}
				if (this.couform.countera < 0 || this.couform.countera >= 1) {
					uni.hideLoading()
					uni.showToast({
						title: 'A参数错误',
						icon: 'success',
						mask: true,
						duration: 2000
					});
					return
				}
				if (this.couform.counterb < 0 || this.couform.counterb >= 1) {
					uni.hideLoading()
					uni.showToast({
						title: 'B参数错误',
						icon: 'success',
						mask: true,
						duration: 2000
					});
					return
				}
				if (this.couform.counterr < 1 || this.couform.counterr >= 100) {
					uni.hideLoading()
					uni.showToast({
						title: 'C参数错误',
						icon: 'success',
						mask: true,
						duration: 2000
					});
					return
				}
				this.fanti = true
				setTimeout(() => {
					console.log('停止循环');
					if (this.fanti) {
						this.fanti = false
						uni.hideLoading()
						uni.showToast({
							title: '修改失败',
							icon: 'fail',
							mask: true,
							duration: 2000
						});
						return
					}
				}, 10000)
				this.modifyup()

			},
			// 修改蓝牙
			modifyup() {

				uni.showLoading({
					title: '修改中',
					mask: true
				});
				let iditem = this.modifynum
				// a系数
				let aa = this.couform.countera * 10000
				let abraa = aa.toFixed(0) * 1
				let straa = abraa.toString(16).split('')
				if (straa.length == 1) {
					this.countcrca = '00'
					this.countcrcb = '0' + straa[0]
				}
				if (straa.length == 2) {
					this.countcrca = '00'
					this.countcrcb = straa[0] + straa[1]
				}
				if (straa.length == 3) {
					this.countcrca = '0' + straa[0]
					this.countcrcb = straa[1] + straa[2]
				}
				if (straa.length == 4) {
					this.countcrca = straa[0] + straa[1]
					this.countcrcb = straa[2] + straa[3]
				}

				// b系数
				let bb = this.couform.counterb * 10000
				let abrbb = bb.toFixed(0) * 1
				let strbb = abrbb.toString(16).split('')
				if (strbb.length == 1) {
					this.countcrcc = '00'
					this.countcrcd = '0' + strbb[0]
				}
				if (strbb.length == 2) {
					this.countcrcc = '00'
					this.countcrcd = strbb[0] + strbb[1]
				}
				if (strbb.length == 3) {
					this.countcrcc = '0' + strbb[0]
					this.countcrcd = strbb[1] + strbb[2]
				}
				if (strbb.length == 4) {
					this.countcrcc = strbb[0] + strbb[1]
					this.countcrcd = strbb[2] + strbb[3]
				}

				// r系数
				let rr = this.couform.counterr * 1
				let abrrr = rr.toFixed(0) * 1
				let strrr = abrrr.toString(16).split('')
				if (strrr.length == 1) {
					this.countcrce = '00'
					this.countcrcf = '0' + strrr[0]
				}
				if (strrr.length == 2) {
					this.countcrce = '00'
					this.countcrcf = strrr[0] + strrr[1]
				}
				if (strrr.length == 3) {
					this.countcrce = '0' + strrr[0]
					this.countcrcf = strrr[1] + strrr[2]
				}
				if (strrr.length == 4) {
					this.countcrce = strrr[0] + strrr[1]
					this.countcrcf = strrr[2] + strrr[3]
				}
				// let msgall = ['','0x03','0x10','0x00','0x00','0x06','','']
				this.jis = this.resHexitem + '10' + '10' + iditem + '00' + '06' + '0c' + this.countcrca + this.countcrcb +
					this.countcrcc + this.countcrcd + this.countcrce + this.countcrcf + '00' + '00' + '00' + '00' + '00' +
					'00'

				this.crc16up(this.jis)
				this.countall[0] = '0x' + this.resHexitem
				this.countall[3] = '0x' + iditem
				this.countall[7] = '0x' + this.countcrca
				this.countall[8] = '0x' + this.countcrcb
				this.countall[9] = '0x' + this.countcrcc
				this.countall[10] = '0x' + this.countcrcd
				this.countall[11] = '0x' + this.countcrce
				this.countall[12] = '0x' + this.countcrcf
				this.countall[19] = '0x' + this.cr[0]
				this.countall[20] = '0x' + this.cr[1]


				setTimeout(() => {
					if (this.countall[0] !== '') {
						const buffer = new ArrayBuffer(21)
						const dataView = new DataView(buffer)
						dataView.setUint8(0, this.countall[0]);
						dataView.setUint8(1, this.countall[1]);
						dataView.setUint8(2, this.countall[2]);
						dataView.setUint8(3, this.countall[3]);
						dataView.setUint8(4, this.countall[4]);
						dataView.setUint8(5, this.countall[5]);
						dataView.setUint8(6, this.countall[6]);
						dataView.setUint8(7, this.countall[7]);
						dataView.setUint8(8, this.countall[8]);
						dataView.setUint8(9, this.countall[9]);
						dataView.setUint8(10, this.countall[10]);
						dataView.setUint8(11, this.countall[11]);
						dataView.setUint8(12, this.countall[12]);
						dataView.setUint8(13, this.countall[13]);
						dataView.setUint8(14, this.countall[14]);
						dataView.setUint8(15, this.countall[15]);
						dataView.setUint8(16, this.countall[16]);
						dataView.setUint8(17, this.countall[17]);
						dataView.setUint8(18, this.countall[18]);
						dataView.setUint8(19, this.countall[19]);
						dataView.setUint8(20, this.countall[20]);
						this.sendMsgToKey(buffer)
					}
				}, 500)
			},
			// 分包
			/**
			 * 向设备发送消息(分包发送，单包20字节，递归发送)
			 */
			sendMsgToKey(buffer) {
				var that = this //vue中的this
				const packageSize = 20 //分包大小
				if (buffer.byteLength <= 20) { //如果小于20直接发送，不再继续调用
					uni.writeBLECharacteristicValue({
						// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
						deviceId: that.deviceId,
						// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
						serviceId: that.serviceId,
						// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
						characteristicId: that.fff, //第二步写入的特征值
						// 这里的value是ArrayBuffer类型
						value: buffer,
						writeType: 'write',
						success: (res) => {
							//此时设备已接收到你写入的数据
							setTimeout(() => {
								if (this.fanti) {
									this.modifyup()
								}
							}, 500)

						},
						fail: (err) => {
							if (err.errCode == 10006) {
								uni.hideLoading()
								uni.showToast({
									title: '当前连接已断开',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
							if (err.errCode == 10004) {
								uni.hideLoading()
								uni.showToast({
									title: '未找到服务',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
							if (err.errCode == 10005) {
								uni.hideLoading()
								uni.showToast({
									title: '未找到特征值',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
						},
						complete: function() {}
					})
				} else { //如果大于20发送完成后继续递归调用
					var newData = buffer.slice(20)
					var writeBuffer = buffer.slice(0, 20)
					uni.writeBLECharacteristicValue({
						// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
						deviceId: that.deviceId,
						// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
						serviceId: that.serviceId,
						// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
						characteristicId: that.fff, //第二步写入的特征值
						// 这里的value是ArrayBuffer类型
						value: writeBuffer,
						writeType: 'write',
						success: function(res) {
							//写入成功后继续递归调用发送剩下的数据
							that.sendMsgToKey(newData)
						},
						fail: (err) => {
							if (err.errCode == 10006) {
								uni.hideLoading()
								uni.showToast({
									title: '当前连接已断开',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
							if (err.errCode == 10004) {
								uni.hideLoading()
								uni.showToast({
									title: '未找到服务',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
							if (err.errCode == 10005) {
								uni.hideLoading()
								uni.showToast({
									title: '未找到特征值',
									icon: 'success',
									mask: true,
									duration: 2000
								});
							}
						},
						complete: function() {}
					})
				}

			},
		}
	}
</script>

<style scoped lang="scss">
	.abr {
		display: flex;
		justify-content: space-between;
		padding: 0rpx 30rpx 10rpx 30rpx;

		.abr-l {
			width: 710rpx;
			margin-top: 20rpx;
			padding: 20rpx 0;
			background-color: #ffffff;
			border-radius: 10rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
			box-shadow: 0 0 10rpx 10rpx rgba(33, 81, 86, 0.1);

			.abritem {
				display: flex;
				align-items: center;
				margin-top: 20rpx;

				.abrtext {
					width: 200rpx;
					font-size: 40rpx;
					color: #327983;
				}
			}
		}
	}

	.bott {
		text-align: center;
	}

	.formula {
		width: 750rpx;
		margin-top: 20rpx;
		margin-bottom: 50rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.formulatop {
			width: 650rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 40rpx;
			border-radius: 15rpx 15rpx 0 0;
			background-color: #327983;
			color: #ffffff;
		}

		.formulabom {
			width: 650rpx;
			height: 280rpx;
			padding: 10rpx 0;
			text-align: center;
			line-height: 70rpx;
			font-size: 30rpx;
			border-radius: 0 0 15rpx 15rpx;
			background-color: #b0ddd4;
			color: #ffffff;
			display: flex;
			flex-wrap: wrap;
			align-content: space-around;
			justify-content: space-around;

			.formulaitem {
				width: 250rpx;
				height: 70rpx;
				margin: 0rpx;
				line-height: 70rpx;
				border-radius: 10rpx;
				background-color: #ffffff;
				color: #327983;
			}
		}
	}

	.abrinput {
		width: 350rpx;
	}

	// 连接设备
	.condev {
		padding: 0 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.yuanIf {
			width: 200rpx;
			height: 200rpx;
			border-radius: 150rpx;
			background-color: #91d8d2;
			border: #67a6a4 solid 10rpx;
			font-size: 30rpx;
			color: #313131;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		}

		.yuanbtnIf {
			background-color: #67a6a4;
		}

		.yuan {
			width: 200rpx;
			height: 200rpx;
			border-radius: 150rpx;
			background-color: #327983;
			border: #5beaf7 solid 10rpx;
			font-size: 30rpx;
			color: #ffffff;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		}

		.yuanbtn {
			background-color: #28636b;
		}

		.fang {
			height: 200rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;

			.fangtext {
				width: 300rpx;
				height: 80rpx;
				border-radius: 50rpx;
				background-color: #327983;
				border: #5beaf7 solid 5rpx;
				font-size: 40rpx;
				color: #ffffff;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
			}

			.fangbut {
				background-color: #28636b;
			}

			.fanginput {
				width: 300rpx;
			}
		}
	}

	.abrbutt {
		width: 650rpx;
		margin: 20rpx auto;
		padding: 20rpx;
		border-radius: 20rpx;
		text-align: center;
		border: #ffffff solid 5rpx;
		box-shadow: 0rpx 0rpx 20rpx 5rpx rgba(33, 81, 86, 0.2);

		.abrbox {
			padding: 10rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.abrbutext {
				width: 240rpx;
				background-color: #fafafa;
			}

			.abrflx {
				width: 380rpx;
				height: 200rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				.botn {
					width: 120rpx;
					height: 60rpx;
				}
			}
		}


	}
</style>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>